#ifndef USER_H
#define USER_H

#include <iostream>
#include <vector>
#include <algorithm>

/*
    Mysql数据库用户权限存储在mysql库的user, db, tables_priv,
    columns_priv, procs_priv这几个系统表中，
    MySQL实例启动后就加载到内存中
*/

// 权限枚举
enum Permission {
    OpenDatabase,
    CreateDatabase,
    DropDatabase,
    ShowTable,
    CreateTable,
    AlterTable,
    DropTable,
    CreateIndex,
    DropIndex
};

class User
{
protected:
    std::string username;
    int role; // 角色：超级用户或普通用户,1为超级用户，0为普通用户
    std::vector<Permission> permissions; // 用户权限

public:
    User(const std::string& username, int role) : username(username), role(role) {}

    virtual ~User() {}

    virtual void displayRole() const {
        std::cout << "用户：" << username << "，角色：" << role << std::endl;
    }

    void addPermission(Permission permission) {
        permissions.push_back(permission);
    }

    virtual void assignPermission(User& user, Permission permission) {
        std::cout << "只有超级用户可以分配权限！" << std::endl;
    }

    virtual void revokePermission(User& user, Permission permission) {
        std::cout << "只有超级用户可以剥夺权限！" << std::endl;
    }

    bool hasPermission(Permission permission) const {
        for (const auto& perm : permissions) {
            if (perm == permission) {
                return true;
            }
        }
        return false;
    }

    std::string getUsername() const;
    void setUsername(const std::string &value);

    std::vector<Permission> getPermissions() const;
    void setPermissions(const std::vector<Permission> &value);

    void setPassword(const std::string &value);

    int getRole() const;
    void setRole(int value);

private:
    std::string password;
};

// 超级用户类
class SuperUser : public User {
public:
    SuperUser(const std::string& username) : User(username, 1) {
        // 超级用户拥有所有权限
        addPermission(Permission::OpenDatabase);
        addPermission(Permission::CreateDatabase);
        addPermission(Permission::DropDatabase);
        addPermission(Permission::ShowTable);
        addPermission(Permission::CreateTable);
        addPermission(Permission::AlterTable);
        addPermission(Permission::DropTable);
        addPermission(Permission::CreateIndex);
        addPermission(Permission::DropIndex);
    }

    void assignPermission(User& user, Permission permission) override {
        std::cout << "超级用户 " << username << " 分配了权限给用户 " << user.getUsername() << std::endl;
        user.addPermission(permission);
    }

    void revokePermission(User& user, Permission permission) override {
        std::cout << "超级用户 " << username << " 剥夺了用户 " << user.getUsername() << " 的权限" << std::endl;
        // 实现剥夺权限
        auto it = std::find(user.getPermissions().begin(), user.getPermissions().end(), permission);
        if (it != user.getPermissions().end()) {
            user.getPermissions().erase(it);
        }
    }
};
#endif // USER_H
